package com.yz.jdd.module.settings.precenter;

import android.text.TextUtils;
import android.widget.Toast;

import com.yz.jdd.base.entity.Response_Base;
import com.yz.jdd.base.utils.IntentHandler;
import com.yz.jdd.module.settings.SelectCodeActivity;
import com.yz.jdd.module.settings.SelectDeviceActivity;
import com.yz.jdd.module.settings.model.PayQrCode;
import com.yz.jdd.module.settings.model.VoiceConfig;
import com.yz.jdd.module.settings.view.SelectCodeView;
import com.yz.jdd.network.MySubscriber;
import com.yz.jdd.network.NetWorkRequset;
import com.yz.jdd.network.request.Request_AppLimitSetting;
import com.yz.jdd.network.request.Request_QecodeManage_listNew;
import com.yz.jdd.network.request.Request_deviceQuery;
import com.yz.jdd.network.request.Request_switchLimitationSetting;
import com.yz.jdd.network.response.Response_PayQrCode;
import com.yz.jdd.network.response.Response_PayQrCodeNew;
import com.yz.jdd.network.response.Response_VoiceStatus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SelectCodePresenter {
    SelectCodeView selectCodeView;
    List<PayQrCode> payQrCodes;

    public SelectCodePresenter(SelectCodeView selectCodeView) {
        this.selectCodeView = selectCodeView;
    }


    /**
     * 设置码id
     */
    public void doAppLimitSetting(String shopId, String shopName) {

        selectCodeView.showLoading();
        Request_AppLimitSetting request = new Request_AppLimitSetting();
        final List<String> qrCodeList = new ArrayList<>();
        if (payQrCodes != null) {
            for (PayQrCode payQrCode : payQrCodes) {
                if (payQrCode.isSelected()) {
                    qrCodeList.add(payQrCode.getQrcodeId());
                }
            }
        }

        request.setQrCodeList(qrCodeList);
        request.setShopId(shopId);
        request.setShopName(shopName);
        NetWorkRequset.setAppLimitSetting(request, new MySubscriber() {
            @Override
            public void onSuccess(Response_Base response_baseModel) {
//                Response_VoiceStatus response_voiceStatus= (Response_VoiceStatus) response_baseModel.getResultObject();
                selectCodeView.hidenLoading();
                selectCodeView.finishBackResult(qrCodeList);
            }

            @Override
            public void onNeedLogin(String msg) {
                selectCodeView.hidenLoading();
            }

            @Override
            public void onFail(Response_Base response_baseModel) {
                selectCodeView.hidenLoading();
                String msg = response_baseModel != null && !TextUtils.isEmpty(response_baseModel.getMsg()) ? response_baseModel.getMsg() : "";
                if (!TextUtils.isEmpty(msg)) {
                    selectCodeView.showErroMsg(msg);
                }

            }
        });
    }

    /**
     * 设置码id(新)
     */
    public void doSwitchLimitationSetting() {
        final Request_switchLimitationSetting request_deviceQuery = new Request_switchLimitationSetting();
        request_deviceQuery.setSwitchType(VoiceConfig.VoiceType.QRCODE_VOICE);
        final List<String> qrCodeList = new ArrayList<>();
        if (payQrCodes != null) {
            for (PayQrCode payQrCode : payQrCodes) {
                if (payQrCode.isSelected()) {
                    qrCodeList.add(payQrCode.getQrcodeId());
                }
            }
        }
        request_deviceQuery.setCodeList(qrCodeList);
        NetWorkRequset.doSwitchLimitationSetting(request_deviceQuery, new MySubscriber() {
            @Override
            public void onSuccess(Response_Base response_baseModel) {
                selectCodeView.hidenLoading();
                selectCodeView.finishBackResult(qrCodeList);
            }

            @Override
            public void onNeedLogin(String msg) {
                selectCodeView.hidenLoading();
//                IntentHandler.startLoginActivity(SelectCodeActivity.this);
            }

            @Override
            public void onFail(Response_Base response_baseModel) {
                selectCodeView.hidenLoading();
                String msg = response_baseModel != null && !TextUtils.isEmpty(response_baseModel.getMsg()) ? response_baseModel.getMsg() : "";
                if (!TextUtils.isEmpty(msg)) {
                    selectCodeView.showErroMsg(msg);
                }
            }
        });
    }


    /**
     * 获取码id 列表
     * shopId=${this.shopId}&currentPage=${this.pageIndex}&pageSize=${this.pageSize}
     */
    public void getQrCodeList(String shopId) {

        Request_QecodeManage_listNew params = new Request_QecodeManage_listNew();
        params.setShopId(shopId);
        params.setPageNo("1");
        params.setPageSize("999");
        NetWorkRequset.getQrCodeList(params, new MySubscriber() {
            @Override
            public void onSuccess(Response_Base response_baseModel) {
                Response_PayQrCode response_payQrCode = (Response_PayQrCode) response_baseModel.getResultObject();
                if (response_payQrCode != null) {
                    payQrCodes = response_payQrCode.getDataList();
                    if (payQrCodes != null) {
                        selectCodeView.showView(payQrCodes);
                    }

                }
            }

            @Override
            public void onNeedLogin(String msg) {

            }

            @Override
            public void onFail(Response_Base response_baseModel) {

            }
        });
    }

    /**
     * 获取码id 列表 根据shopId
     * shopId=${this.shopId}&currentPage=${this.pageIndex}&pageSize=${this.pageSize}
     */
    public void doQueryQrCodesByShopIdList(String shopId) {

        Request_deviceQuery params = new Request_deviceQuery();
        params.shopIdList = new String[]{shopId};
        NetWorkRequset.doQueryQrCodesByShopIdList(params, new MySubscriber() {
            @Override
            public void onSuccess(Response_Base response_baseModel) {
                List<Response_PayQrCodeNew> list = (ArrayList<Response_PayQrCodeNew>) response_baseModel.getResultObject();
                if(list != null && list.size() > 0){
                    payQrCodes = list.get(0).qrCodeList;
                    if (payQrCodes != null) {
                        selectCodeView.showView(payQrCodes);
                    }
                }
            }

            @Override
            public void onNeedLogin(String msg) {

            }

            @Override
            public void onFail(Response_Base response_baseModel) {
                if (!TextUtils.isEmpty(response_baseModel.getMsg())) {
                    selectCodeView.showErroMsg(response_baseModel.getMsg());
                }
            }
        });
    }
}
